package com.company.biz.product.action;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.biz.product.service.SupplierService;
import com.company.biz.user.action.BaseController;
import com.company.product.req.SupplierIdReq;
import com.company.product.req.SupplierIdsReq;
import com.company.product.req.SupplierInfoReq;
import com.company.product.req.SupplierListPageReq;
import com.company.product.resp.SupplierInfoResp;
import com.company.user.entity.UserEntity;
import com.company.user.vo.GeneralResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(description = "供应商信息管理")
@RestController
@RequestMapping("/supplier")
public class SupplierController extends BaseController {

    @Resource
    private SupplierService supplierService;

    @ApiOperation(value = "获取供应商列表分页")
    @RequestMapping(value = "/getSupplierListPage", method = RequestMethod.POST)
    public GeneralResult<Page<SupplierInfoResp>> getSupplierListPage(@RequestBody SupplierListPageReq req) {
        Page<SupplierInfoResp> supplierListPage = supplierService.getSupplierListPage(req);
        return GeneralResult.genSuccessResult(supplierListPage);
    }


    @ApiOperation(value = "添加供应商信息")
    @RequestMapping(value = "/addSupplier", method = RequestMethod.POST)
    public GeneralResult<String> addSupplier(@RequestBody SupplierInfoReq req) {
        UserEntity userEntity = getUserEntity();
        boolean b = supplierService.addSupplier(req, userEntity);
        return GeneralResult.genSuccessResult(b ? "添加成功" : "添加失败");
    }


    @ApiOperation(value = "通过供应商id获取供应商信息")
    @RequestMapping(value = "/getSupplierBySupplierId", method = RequestMethod.POST)
    public GeneralResult<SupplierInfoResp> getSupplierBySupplierId(@RequestBody SupplierIdReq req) {
        SupplierInfoResp supplier = supplierService.getSupplierBySupplierId(req.getSupplierId());
        return GeneralResult.genSuccessResult(supplier);
    }

    @ApiOperation(value = "修改供应商信息")
    @RequestMapping(value = "/updateSupplierBySupplierId", method = RequestMethod.POST)
    public GeneralResult<String> updateSupplierBySupplierId(@RequestBody SupplierInfoReq req) {
        UserEntity userEntity = getUserEntity();
        boolean b = supplierService.updateSupplierBySupplierId(req,userEntity);
        return GeneralResult.genSuccessResult(b ? "修改成功" : "修改失败");
    }

    @ApiOperation(value = "删除供应商通过供应商id")
    @RequestMapping(value = "/deleteSupplierBySupplierIds", method = RequestMethod.POST)
    public GeneralResult<String> deleteSupplierBySupplierIds(@RequestBody SupplierIdsReq req) {
        UserEntity userEntity = getUserEntity();
        boolean b = supplierService.deleteSupplierBySupplierIds(req.getSupplierIds(),userEntity);
        return GeneralResult.genSuccessResult(b ? "删除成功" : "删除失败");
    }

}
